package cn.fintecher.pangolin.service.business.web;

import cn.fintecher.pangolin.common.enums.PayCheckStatus;
import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.elastic.CaseFollowupRecord;
import cn.fintecher.pangolin.entity.elastic.PaymentRecord;
import cn.fintecher.pangolin.entity.mysql.PaymentCheck;
import cn.fintecher.pangolin.service.business.model.request.AppAddFollowRecordRequest;
import cn.fintecher.pangolin.service.business.model.request.FindAppCaseRequest;
import cn.fintecher.pangolin.service.business.model.response.AppBaseCaseResponse;
import cn.fintecher.pangolin.service.business.model.response.AppCaseInfoResponse;
import cn.fintecher.pangolin.service.business.model.response.FindAppCaseResponse;
import cn.fintecher.pangolin.service.business.model.response.FindCaseResponse;
import cn.fintecher.pangolin.service.business.service.AppMainService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Auther: xiaqun
 * @Description: app主业务相关接口
 * @Date: 10:43 2019/11/14
 */

@RestController
@RequestMapping("/api/appMainController")
@Api(value = "app主业务相关接口", description = "app主业务相关接口")
public class AppMainController extends BaseController {
    @Autowired
    private AppMainService appMainService;

    /**
     * app协催案件列表查询
     */
    @ApiOperation(value = "app协催案件列表查询", notes = "app协催案件列表查询")
    @GetMapping("/getAllCase")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<FindAppCaseResponse>> getAllCase(FindAppCaseRequest request, Pageable pageable,
                                                                @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to get all case,{}", request);
        Page<FindAppCaseResponse> page = appMainService.getAllCase(request, pageable, token);
        return ResponseEntity.ok().body(page);
    }

    /**
     * app外访案件列表查询
     */
    @ApiOperation(value = "app外访案件列表查询", notes = "app外访案件/列表查询")
    @GetMapping("/getAllOutCase")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<FindAppCaseResponse>> getAllOutCase(FindAppCaseRequest request, Pageable pageable,
                                                                   @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to get all out case,{}", request);
        Page<FindAppCaseResponse> page = appMainService.getAllOutCase(request, pageable, token);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 获取基础信息
     */
    @ApiOperation(value = "获取基础信息", notes = "获取基础信息")
    @GetMapping("/getBaseInfo")
    public ResponseEntity<AppBaseCaseResponse> getBaseInfo(@RequestParam @ApiParam(value = "案件ID", required = true) String caseId) {
        log.info("REST request to get base info,{}", caseId);
        AppBaseCaseResponse response = appMainService.getBaseInfo(caseId);
        return ResponseEntity.ok().body(response);
    }

    /**
     * 获取共债信息
     */
    @ApiOperation(value = "获取共债信息", notes = "获取共债信息")
    @GetMapping("/getCaseInfo")
    public ResponseEntity<AppCaseInfoResponse> getCaseInfo(@RequestParam @ApiParam(value = "案件ID", required = true) String caseId) {
        log.info("REST request to get case info,{}", caseId);
        AppCaseInfoResponse response = appMainService.getCaseInfo(caseId);
        return ResponseEntity.ok().body(response);
    }

    /**
     * 对账单查询
     */
    @ApiOperation(value = "对账单查询", notes = "对账单查询")
    @GetMapping("/caseBillRecord")
    public ResponseEntity<List<PaymentRecord>> caseBillRecord(@RequestParam @ApiParam(value = "案件ID", required = true) String caseId) {
        log.info("REST request to case bill record,{}", caseId);
        List<PaymentRecord> list = appMainService.caseBillRecord(caseId);
        return ResponseEntity.ok().body(list);
    }

    /**
     * 查询外访催记
     */
    @ApiOperation(value = "查询外访催记", notes = "查询外访催记")
    @GetMapping("/getFollowRecord")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<CaseFollowupRecord>> getFollowRecord(@RequestParam @ApiParam(value = "证件号", required = true) String certificateNo,
                                                                    Pageable pageable, @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to get followup record,{}", certificateNo);
        Page<CaseFollowupRecord> page = appMainService.getFollowRecord(certificateNo, pageable);
        return ResponseEntity.ok().body(page);
    }

    /**
     * 保存催记
     */
    @ApiOperation(value = "保存催记", notes = "保存催记")
    @PostMapping("/saveFollowRecord")
    public ResponseEntity<Void> saveFollowRecord(@RequestBody AppAddFollowRecordRequest request,
                                                 @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to save follow record,{}", request);
        appMainService.saveFollowRecord(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改头像
     */
    @ApiOperation(value = "修改头像", notes = "修改头像")
    @GetMapping("/modifyPhoto")
    public ResponseEntity<Map<String, String>> modifyPhoto(@RequestParam @ApiParam(value = "url", required = true) String url,
                                                           @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to modify photo,{}", url);
        appMainService.modifyPhoto(url, token);
        Map<String, String> map = new HashMap<>();
        map.put("url", url);
        return ResponseEntity.ok().body(map);
    }

    /**
     * 查询PTT/CP记录
     */
    @ApiOperation(value = "查询PTT/CP记录", notes = "查询PTT/CP记录")
    @GetMapping("/getAllRecord")
    public ResponseEntity<List<PaymentCheck>> getAllRecord(@RequestParam @ApiParam(value = "案件ID", required = true) String caseId,
                                                           @RequestParam @ApiParam(value = "类型", required = true) PayCheckStatus status) {
        log.info("REST request to get all record,{},{}", caseId, status);
        List<PaymentCheck> list = appMainService.getAllRecord(caseId, status);
        return ResponseEntity.ok().body(list);
    }
}
